﻿Public Class PhanLoaiThuChi
    Public DSPLThuChi As DataTable = PhanLoaiThuChiDAO.GetAllPhanLoaiThuChi()
    Public Sub GanTenColumn()
        DanhSachPhanLoaiThu.DataSource = DSPLThuChi
        DanhSachPhanLoaiThu.Columns(0).HeaderText = "Mã"
        DanhSachPhanLoaiThu.Columns(0).Width = 95
        DanhSachPhanLoaiThu.Columns(1).HeaderText = "Tên Phân Loại"
        DanhSachPhanLoaiThu.Columns(1).Width = 200
        DanhSachPhanLoaiThu.Columns(2).HeaderText = "Người Tạo"
        DanhSachPhanLoaiThu.Columns(2).Width = 100
        DanhSachPhanLoaiThu.Columns(3).Visible = False
    End Sub

    Private Sub PhanLoaiThuChi_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        GanTenColumn()
        If MainForm.current_user.QuyenHan = 1 Then
            bt_xoa.Visible = True
        Else
            bt_xoa.Visible = False
        End If
    End Sub

    Private Sub bt_dong_Click(sender As System.Object, e As System.EventArgs) Handles bt_dong.Click
        Me.Close()
    End Sub

    Private Sub bt_them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_them.Click

        ThaoTacPLThuChi.AddTitle("Thêm Đối Tượng Mới")
        ThaoTacPLThuChi.CleanTextBox()
        ThaoTacPLThuChi.AddUsername(MainForm.current_user.username, MainForm.current_user.MaUser)
        ThaoTacPLThuChi.ShowDialog()
    End Sub

    Private Sub bt_sua_Click(sender As System.Object, e As System.EventArgs) Handles bt_sua.Click
        Dim index As Integer = DanhSachPhanLoaiThu.CurrentCell.RowIndex
        ThaoTacPLThuChi.index = index
        Dim pl As PLThuChi = New PLThuChi()
        pl.id = DSPLThuChi.Rows(index).Item(0)
        pl.ten = DSPLThuChi.Rows(index).Item(1)
        pl.username = DSPLThuChi.Rows(index).Item(2)
        pl.id_user = DSPLThuChi.Rows(index).Item(3)
        ThaoTacPLThuChi.AddTitle("Chỉnh Sửa Đối Tượng")
        ThaoTacPLThuChi.EditItem(pl)
        ThaoTacPLThuChi.ShowDialog()
    End Sub

    Private Sub bt_xoa_Click(sender As System.Object, e As System.EventArgs) Handles bt_xoa.Click
        Dim index As Integer = DanhSachPhanLoaiThu.CurrentCell.RowIndex
        Dim ans As DialogResult = MessageBox.Show("Bạn thật sự muốn xóa đối tượng này? ", "Thông Báo", MessageBoxButtons.YesNo)
        If ans = Windows.Forms.DialogResult.Yes Then
            Dim result As Integer = PhanLoaiThuChiDAO.DeleteItem(DSPLThuChi.Rows(index).Item(0))
            If result = 1 Then
                DSPLThuChi.Rows.RemoveAt(index)
            Else
                MessageBox.Show("Item chưa được xóa khỏi Database, Chương trình sẽ tự động đóng và mở lại cửa sổ này.", "Thông Báo", MessageBoxButtons.OK)
                Me.Close()
                Me.MdiParent = MainForm
                Me.Show()
            End If
        Else
            Return
        End If
    End Sub
    Private Sub DanhSachPhanLoaiThu_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DanhSachPhanLoaiThu.CellDoubleClick
        bt_sua_Click(Nothing, Nothing)
    End Sub
End Class